package com.sea.physical.controller;

import com.codecrane.core.power.service.DictDetailService;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.physical.entity.Cabinet;
import com.sea.physical.entity.Coefficient;
import com.sea.physical.service.CoefficientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(value = "/sea/physical/coefficient")
public class CoefficientController {

    @Autowired
    private CoefficientService coefficientService;

    @Autowired
    private DictDetailService dictDetailService;

    /**
     * 管理主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        //国家字典
        model.addAttribute("country",dictDetailService.findByTypeCode("base_country", null));
        return "/sea/physical/coefficient/main";
    }

    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     Coefficient coefficient) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<Coefficient> list = coefficientService.findByCnd(coefficient);
        PageInfo<Coefficient> page = new PageInfo<Coefficient>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 保存
     * @param coefficient
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxReturn save(Coefficient coefficient) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try{
            if (null != coefficient && coefficientService.save(coefficient) > 0) {
                result.setOk(true).setMsg("保存成功！");
            }
        }catch (DuplicateKeyException e){
            result.setMsg("当前国家系数已存在!");
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 修改
     * @param coefficient
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(Coefficient coefficient) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != coefficient && coefficientService.usave(coefficient) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxReturn delete(Integer id) {
        AjaxReturn result = Ajax.fail().setMsg("删除失败！");
        try{
            if (null != id && coefficientService.delete(id) > 0) {
                result.setOk(true).setMsg("删除成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }
}
